from rest_framework.generics import ListAPIView
from rest_framework .response import Response
from utils.response import APIResponse
from . import models, serializers
from django.core.cache import cache

# 访问量大，且数据较固定的接口，建议建立接口缓存
from settings.const import *
class BannerListAPIView(ListAPIView):
    queryset = models.Banner.objects.filter(is_delete=False, is_show=True).order_by('-orders')[:BANNER_COUNT]
    serializer_class = serializers.BannerModelSerializer

    # 缓存有,走缓存,缓存没有走数据库
    def list(self, request, *args, **kwargs):
        banner_data = cache.get('banner_list')
        if not banner_data:
            print('走数据库')
            response = super().list(request,*args,**kwargs)
            banner_data = response.data
            # 建立缓存，不建议设置缓存过期时间，用celery等框架后台异步更新缓存即可
            cache.set('banner_list',banner_data)
        return Response(banner_data)